layui.use(['table'],function () {
    var table = layui.table;
    table.render({
        elem:'#upload'
        ,size:'lg'
        ,url:'/admin/cloud/list_vo'
        ,cols:[[
            {field:'name',title:'上传名称',}
            ,{field:'key',title:'key',}
            ,{field:'createTime',title:'创建时间',}
            ,{title: "操作",fixed:'right',align:'center',toolbar:'#bar'}
        ]]
        ,page:true
    })
    ,table.on('tool(upload)', function(obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
        var id = data.id;
        if (layEvent == "select"){
            $.ajax({
                url:'/admin/cloud/get_config'
                ,method:'get'
                ,data:{id:id}
                ,success:(data) =>{
                    console.log(data)
                    if (id == 1){
                        $("#sel_qny_ak").val(data.accessKey);
                        $("#sel_qny_sk").val(data.secretKey);
                        $("#sel_qny_bn").val(data.bucketName);
                        $("#sel_qny_dm").val(data.domain);
                        $("#sel_qny_pf").val(data.prefix);
                        console.log(data.accessKey)
                        var qiniuyun = layer.open({
                            type:1
                            ,area:['500px','500px']
                            ,title:"七牛云配置"
                            ,content:$("#sel_qiniuyun")
                            ,shade:0
                            ,btn:[]
                            ,cancel: function (layuio,index) {
                                layer.close(qiniuyun)

                            }
                        })

                    }else if (id == 2){
                        $("#sel_ali_aki").val(data.accessKeyId);
                        $("#sel_ali_aks").val(data.accessKeySecret);
                        $("#sel_ali_bn").val(data.bucketName);
                        $("#sel_ali_dm").val(data.domain);
                        $("#sel_ali_pf").val(data.prefix);
                        $("#sel_ali_ep").val(data.endPoint)
                        console.log(data.bucketName)
                        var qiniuyun = layer.open({
                            type:1
                            ,area:['500px','500px']
                            ,title:"阿里云配置"
                            ,content:$("#sel_ali")
                            ,shade:0
                            ,btn:[]
                            ,cancel: function (layuio,index) {
                                layer.close(qiniuyun)

                            }
                        })
                    }else if(id == 3){
                        alert("本地无法查看")
                    }
                }
            })
        }else if(layEvent == "update"){
            $.ajax({
                url:'/admin/cloud/get_config'
                ,method:'get'
                ,data:{id:id}
                ,success:(data) =>{
                    console.log(data)
                    if (id == 1){
                        $("#upd_qny_ak").val(data.accessKey);
                        $("#upd_qny_sk").val(data.secretKey);
                        $("#upd_qny_bn").val(data.bucketName);
                        $("#upd_qny_dm").val(data.domain);
                        $("#upd_qny_pf").val(data.prefix);
                        console.log(data.accessKey)
                        var qiniuyun = layer.open({
                            type:1
                            ,area:['500px','500px']
                            ,title:"七牛云配置"
                            ,content:$("#upd_qiniuyun")
                            ,shade:0
                            ,btn:["修改","取消"]
                            ,btn1:function(index,layero){
                                var accessKey = $("#upd_qny_ak").val();
                                var secretKey = $("#upd_qny_sk").val()
                                var bucketName = $("#upd_qny_bn").val()
                                var domain = $("#upd_qny_dm").val()
                                var prefix = $("#upd_qny_pf").val()
                                var formData = {
                                     accessKey:accessKey
                                    ,secretKey:secretKey
                                    ,bucketName:bucketName
                                    ,domain:domain
                                    ,prefix:prefix
                                }
                                $.ajax({
                                    url:'/admin/cloud/qiniuyun_config'
                                    ,method:'post'
                                    ,data:formData
                                    ,success:(response) =>{
                                        if (response.code == 0) {
                                            alert("修改七牛云配置成功")
                                            location.reload()
                                        } else {
                                            alert(response.msg)
                                            return false
                                        }
                                    }
                                })
                            }
                            ,cancel: function (layuio,index) {
                                layer.close(qiniuyun)

                            }
                        })

                    }else if (id == 2){
                        $("#upd_ali_aki").val(data.accessKeyId);
                        $("#upd_ali_aks").val(data.accessKeySecret);
                        $("#upd_ali_bn").val(data.bucketName);
                        $("#upd_ali_dm").val(data.domain);
                        $("#upd_ali_pf").val(data.prefix);
                        $("#upd_ali_ep").val(data.endPoint)
                        console.log(data.bucketName)
                        var qiniuyun = layer.open({
                            type:1
                            ,area:['500px','500px']
                            ,title:"阿里云配置"
                            ,content:$("#upd_ali")
                            ,shade:0
                            ,btn:["修改","取消"]
                            ,btn1:function(index,layero){
                                var accessKeyId = $("#upd_ali_aki").val();
                                var accessKeySecret = $("#upd_ali_aks").val()
                                var bucketName = $("#upd_ali_bn").val()
                                var domain = $("#upd_ali_dm").val()
                                var prefix = $("#upd_ali_pf").val()
                                var endPoint = $("#upd_ali_ep").val()
                                var formData = {
                                     accessKeyId:accessKeyId
                                    ,accessKeySecret:accessKeySecret
                                    ,bucketName:bucketName
                                    ,domain:domain
                                    ,prefix:prefix
                                    ,endPoint:endPoint
                                }

                                $.ajax({
                                    url:'/admin/cloud/aliyun_config'
                                    ,method:'post'
                                    ,data:formData
                                    ,success:(response) =>{
                                        if (response.code == 0) {
                                            alert("修改阿里云配置成功")
                                            console.log(data)
                                            location.reload()
                                        } else {
                                            alert(response.msg)
                                            return false
                                        }
                                    }
                                })
                            }
                            ,cancel: function (layuio,index) {
                                layer.close(qiniuyun)
                            }
                        })
                    }else if(id == 3){
                        alert("本地无法修改")
                    }
                }
            })
        }else if(layEvent == "do"){
            console.log(data)
           if (data.use == false){
               var id = data.id
               $.ajax({
                   url:'/admin/cloud/save'
                   ,method:'post'
                   ,data:{id:id}
                   ,success:(response)=>{
                       if (response.code == 0) {
                           alert("使用成功")
                           location.reload()
                       } else {
                           alert(response.msg)
                           return false
                       }
                   }
               })
           }else{
               alert("您正在使用本配置")
           }
        }
    })
})

//
// $(function () {
//     var set_role1 = document.getElementById("set_role1")
//     set_role1.onclick = function () {
//         alert("aa")
//         $("#set_role1").css('display','none');
//         $("#noSet_role1").css('display','')
//         $("#set_role2").css('display','');
//         $("#noSet_role2").css('display','none')
//
//     }
//     var set_role2 = document.getElementById("set_role2")
//     set_role2.onclick = function () {
//         $("#set_role1").css('display','');
//         $("#noSet_role1").css('display','none')
//         $("#set_role2").css('display','none');
//         $("#noSet_role2").css('display','')
//
//     }
// })
// $(function () {
//     $("#update").click(function () {
//         var upd = layer.open({
//             type:1,
//             area:['350px','460px'],
//             title: "修改配置"
//             ,content: $("#upd_category"),
//             shade: 0,
//             btn: ['提交', '重置']
//             ,btn1: function(index, layero){//提交按钮
//
//             },
//             btn2: function(index, layero){//重置按钮
//
//             },
//             cancel: function(layero,index){//关闭按钮
//                 layer.close(upd);
//             }
//         });
//     })
//
// })